package qunchen.com.mytestapplication.bluetoothspp2;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.inuker.bluetooth.library.search.SearchRequest;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;

import org.json.JSONArray;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import qunchen.com.mytestapplication.R;
import qunchen.com.mytestapplication.account.LoginActivity;
import qunchen.com.mytestapplication.ble.BleAdapter;
import qunchen.com.mytestapplication.ble.BleDetailActivity;
import qunchen.com.mytestapplication.ble.BleTool;
import qunchen.com.mytestapplication.ble.BledefDetailActivity;
import qunchen.com.mytestapplication.utils.ItemData;
import qunchen.com.mytestapplication.utils.ToastUtils;

/***
 * 搜索蓝牙
 */
public class BleSppSearchActivity extends BleBaseActivity {

    private ListView mLvBle;
    private ImageView mImgSwitch;
    private TextView mTvTitle;
    private BluetoothAdapter BTAdapter;

    private List<BluetoothDevice> mDeviceList = new ArrayList<>();//2.0蓝牙列表
    private List<SearchResult> mBleList = new ArrayList<>();//蓝牙列表
    private SPPAdapter mAdapter;
    private BleAdapter mBleAdapter;
    private int mVersionState = 2;//当前版本，1：2.0蓝牙； 2：4.0蓝牙
    private boolean isRegister = false;
//    private List<ItemData> mItemBleList = new ArrayList<>();//自己记录蓝牙列表
    //以前保存的list
    List<ItemData> preExistlist;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble_spp_search);


//                initializationMBleList();

        mLvBle = (ListView) findViewById(R.id.lv_ble);
        mAdapter = new SPPAdapter(this);
        mAdapter.setData(mDeviceList);
        mBleAdapter = new BleAdapter(this);
        mBleAdapter.setData(mBleList);
        mTvTitle = (TextView) findViewById(R.id.tv_title);

        mLvBle.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mVersionState == 1) {
                    if (BTAdapter != null)
                        BTAdapter.cancelDiscovery();
                    if (mDeviceList.size() < i)
                        return;
//                showToast("点击了第 " + i + " 个");
                    BluetoothDevice device = mDeviceList.get(i);
                    bondBT(device);
                } else {
                    if (mBleList.size() < i)
                        return;
                    SearchResult device = mBleList.get(i);
                    //暂时关掉
/*                    device.count = device.count-1;
                    System.out.println("i = "+i+",new count "+ device.count);
                    System.out.println("点击 mItemBleList = "+preExistlist.toString());
//                    preExistlist.get(i).setRestcount( device.count);
                    //点击计数 保存
                    preExistlist.get(preExistlist.indexOf(new ItemData(device.getName()))).setRestcount( device.count);
                    mBleAdapter.notifyDataSetChanged();*/
                    if(device.getStatus()==0){
                        ToastUtils.showToast(mContent,"蓝牙不在附近");
                        return;
                    }
//                    BledefDetailActivity.startActivity(BleSppSearchActivity.this, device.getAddaddress(), device.getAddName());
                        BleDetailActivity.startActivity(BleSppSearchActivity.this, device.getAddaddress(), device.getAddName());
//                    BleDetailActivity.startActivity(BleSppSearchActivity.this, device.getAddress(), device.getName());
                }

            }
        });
        if (Build.VERSION.SDK_INT >= 6.0) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    1000);
        }
        findViewById(R.id.img_switch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
/*                mVersionState = mVersionState == 1 ? 2 : 1;
                if (mVersionState == 1) {
                    mTvTitle.setText("SPP设备列表");
                } else {
                    mTvTitle.setText("Ble设备列表");
                }
                search();*/
                LoginActivity.launch(mContent);
            }
        });
        findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();
            }
        });
        search();
    }

    /**
     *  初始化蓝牙列表
     */
    private void initializationMBleList() {
        //获取上次存储的数据
//        mBleList = getAssistData();
        //预存数据
        mBleList.add(new SearchResult("NBee-BA0333255E71"));
        mBleList.add(new SearchResult("NBee-BA0353446671"));
        mBleList.add(new SearchResult("NBee-BA0351355E71"));
        mBleList.add(new SearchResult("NBee-BA034B145E71"));
        mBleList.add(new SearchResult("NBee-BA0336115E71"));
        mBleList.add(new SearchResult("NBee-BA03252C5E71"));
        mBleList.add(new SearchResult("NBee-BA03392F6671"));


    }

    private void search() {
        if (mVersionState == 1) {
            BleTool.getClient().stopSearch();
            mLvBle.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
            initSPP();
            searchSPP();
        } else {
            initSPP();
//            releaseSPP();
            searchBle();
            mLvBle.setAdapter(mBleAdapter);
            mBleAdapter.notifyDataSetChanged();
        }
    }


    /***
     * 初始化SPP
     */
    private void initSPP() {
        if (!isRegister) {
            registerBTReceiver();
        }
        checkBle();
        isRegister = true;
    }

    /***
     * 释放SPP
     */
    private void releaseSPP() {
        if (BTAdapter != null) {
            BTAdapter.cancelDiscovery();
        }
        if (isRegister) {
            unregisterReceiver(BTReceive);
        }
        isRegister = false;
    }

    /**
     * 注册广播
     */
    public void registerBTReceiver() {
        // 设置广播信息过滤
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);   //发现蓝牙事件
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//蓝牙扫描结束事件
//        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//状态改变
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        // 注册广播接收器，接收并处理搜索结果
        registerReceiver(BTReceive, intentFilter);
    }

    /***
     * 检查蓝牙
     */
    public void checkBle() {
        BTAdapter = BluetoothAdapter.getDefaultAdapter();
        mDeviceList.clear();

        mAdapter.notifyDataSetChanged();
        if (BTAdapter != null) {
            if (!BTAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                // 设置蓝牙可见性，最多300秒
                intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                startActivity(intent);
            } else {
                addPairedDevice();
//                BTAdapter.startDiscovery();
            }
        } else {
            Toast.makeText(this, "本地设备驱动异常!", Toast.LENGTH_SHORT).show();
        }
    }


    /***
     * 搜索SPP设备
     */
    public void searchSPP() {
        if (BTAdapter == null) {
            return;
        }
        if (BTAdapter.isDiscovering()) {
            return;
        }
        BTAdapter.startDiscovery();
    }

    /**
     * 广播接收者
     */
    private BroadcastReceiver BTReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mVersionState == 2) {
                return;
            }
            String action = intent.getAction();
            //打印Action，调试使用
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                Log.e("BroadcastReceiver", "BluetoothDevice.ACTION_FOUND");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                for (BluetoothDevice d : mDeviceList) {
                    if (TextUtils.isEmpty(d.getAddress()) || d.getAddress().equalsIgnoreCase(device.getAddress())) {
                        return;
                    }
                }
                mDeviceList.add(device);
                mAdapter.notifyDataSetChanged();
            } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
                BluetoothDevice blnDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                setProgressMsg("正在配对..");
                switch (blnDevice.getBondState()) {
                    case BluetoothDevice.BOND_BONDING://正在配对
                        setProgressMsg("正在配对..");
                        break;
                    case BluetoothDevice.BOND_BONDED://配对结束
                        mAdapter.notifyDataSetChanged();
                        setProgressMsg("配对完成,开始连接..");
                        startActivity(blnDevice);
                        break;
                    case BluetoothDevice.BOND_NONE://取消配对/未配对
                        showToast("配对失败!");
                        for (BluetoothDevice d : mDeviceList) {
                            if (TextUtils.isEmpty(d.getAddress()) && d.getAddress().equalsIgnoreCase(blnDevice.getAddress())) {
                                mAdapter.notifyDataSetChanged();
                                return;
                            }
                        }
                        dismissProgressDialog();
                    default:
                        break;
                }
            } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                Log.e("BroadcastReceiver", "BluetoothAdapter.ACTION_BOND_STATE_CHANGED");
            }
        }
    };

    /**
     * 绑定蓝牙
     */
    private void bondBT(BluetoothDevice device) {
        showProgressDialog("配对蓝牙开始");
        // 搜索蓝牙设备的过程占用资源比较多，一旦找到需要连接的设备后需要及时关闭搜索
        // 获取蓝牙设备的连接状态
        int connectState = device.getBondState();
        switch (connectState) {
            // 未配对
            case BluetoothDevice.BOND_NONE:
                setProgressMsg("开始配对");
                try {
                    Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
                    createBondMethod.invoke(device);
                } catch (Exception e) {
                    showToast("配对失败！");
                    dismissProgressDialog();
                    e.printStackTrace();
                }
                break;
            // 已配对
            case BluetoothDevice.BOND_BONDED:
                //进入连接界面
                startActivity(device);
                break;
        }

    }

    private void startActivity(BluetoothDevice device) {
        dismissProgressDialog();
        Intent intent = new Intent(BleSppSearchActivity.this, SPPOperationActivity.class);
        intent.putExtra("device", device);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    /**
     * 增加配对设备
     */
    private void addPairedDevice() {
        Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String str = device.getName() + "|" + device.getAddress();
                mDeviceList.add(device);
            }
        }
    }

    /***
     * 查找ble蓝牙
     */
    private void searchBle() {
        mBleList.clear();
        initializationMBleList();
        mBleAdapter.notifyDataSetChanged();
        SearchRequest request = new SearchRequest.Builder()
                .searchBluetoothLeDevice(5000, 2)   // 先扫BLE设备3次，每次3s
                .build();
        BleTool.getClient().search(request, new SearchResponse() {
            private Map<String, String> existingMap = new HashMap<>();

            @Override
            public void onSearchStarted() {

            }

            @Override
            public void onDeviceFounded(SearchResult device) {
                String devicename = device.getName();
//                System.out.println("发现的device  ："+devicename+" ,  address  :"+device.getAddress());
                //如果不是以NBee的开头，就退出
                if(!device.getName().startsWith("NBee")){
                    return;
                }
                if (existingMap.containsKey(device.getAddress())) {
                    return;
                }
                existingMap.put(device.getAddress(), device.getAddress());
                //暂时关掉，获取程序数据
                System.out.println("device  加入记忆之前  = " + device.getName());
//                final ItemData itemData = new ItemData(device.getName(), device.getAddress(),device.count);

                System.out.println("上次存取的数据："+mBleList.toString());
                for (SearchResult item : mBleList) {
                    //是否包含找到这个蓝牙

                        //在线的蓝牙,找到这个蓝牙
                        if (TextUtils.equals(item.getAddName(), device.getName())) {
                            //把地址和状态赋给列表的item
                            item.setAddaddress(device.getAddress());
                            item.setStatus(1);
                        }/* else {
                            //不在线的蓝牙
                            item.setStatus(0);
                        }*/
                }
/*                //判断list是否为空
                if(!mBleList.isEmpty()) {
                    //增强for循环遍历list
                    for (SearchResult item : mBleList) {
                        //是否包含找到这个蓝牙
                        if (mBleList.contains(device)) {
                            //在线的蓝牙
                            if (TextUtils.equals(item.getName(), device.getName())) {
                                device.setStatus(1);
                            } else {
                                //不在线的蓝牙
                                device.setStatus(0);
                            }
                        } else {
                            //不包含就加到里面去,设置为在线
                            device.setStatus(1);
                            mBleList.add(device);
                        }
                    }
                }else{
                    //list为空
                    //不包含就加到里面去,设置为在线
                    device.setStatus(1);
                    mBleList.add(device);
                }*/
                System.out.println("device = " + device.getName());

//                mBleList.add(device);
                mBleAdapter.notifyDataSetChanged();
            }

            @Override
            public void onSearchStopped() {

            }

            @Override
            public void onSearchCanceled() {

            }
        });
    }


    @Override
    protected void onStop() {
        super.onStop();
        if (BTAdapter != null)
            BTAdapter.cancelDiscovery();
        BleTool.getClient().stopSearch();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播
        releaseSPP();
        //保存界面数据,保存蓝牙列表
        saveAssistData(mBleList);

//        List<SearchResult>
        //增强for循环遍历list
/*        for(SearchResult device:mBleList){
            System.out.println("保存的数据    name  = "+ device.getName());
        }*/
//        System.out.println("保存的数据"+mBleList.toString());
    }

    //----------------------------------------------------保存、获取界面数据
    private void saveAssistData(List<SearchResult> AssistData) {
//        AssistData.sTimeA = editTextTimeCOMA.getText().toString();
//        AssistData.sTimeB = editTextTimeCOMB.getText().toString();
//        AssistData.sTimeC = editTextTimeCOMC.getText().toString();
//        AssistData.sTimeD = editTextTimeCOMD.getText().toString();
        SharedPreferences msharedPreferences = getSharedPreferences("ComAssistant", Context.MODE_PRIVATE);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            //将List转换成数组
            SearchResult[] obj = new SearchResult[AssistData.size()];
            AssistData.toArray(obj);
            //执行序列化存储
            oos.writeObject(obj);
//            oos.writeObject(AssistData);
            String sBase64 = new String(Base64.encode(baos.toByteArray(),0));
            SharedPreferences.Editor editor = msharedPreferences.edit();
            editor.putString("AssistData", sBase64);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //----------------------------------------------------
    private   List<SearchResult> getAssistData() {
        SharedPreferences msharedPreferences = getSharedPreferences("ComAssistant", Context.MODE_PRIVATE);
        List<SearchResult>  AssistData =	 new ArrayList<>();
        try {
            String personBase64 = msharedPreferences.getString("AssistData", "");
            byte[] base64Bytes = Base64.decode(personBase64.getBytes(),0);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            //执行反序列化读取
            SearchResult[] obj = (SearchResult[]) ois.readObject();
            //将数组转换成List
            List<SearchResult> AssistDataConvert = Arrays.asList(obj);
            if(!AssistDataConvert.isEmpty()){
                AssistData = AssistDataConvert;
            }
//            AssistData =String2List(objectString);
//            AssistData = ( List<SearchResult>) ois.readObject();
//            AssistData.setTxtMode(false);
            return AssistData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AssistData;
    }



}
